﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using KioskViewerBLL.BLLObjects;

public partial class BKCategoryViewer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected int CategoryId
    {
        get { 
            if ( ViewState["BGCategoryId"] == null)
            {
                try
                {
                    if (Request.QueryString["id"] != null)
                        ViewState["BGCategoryId"] = int.Parse(Request.QueryString["id"].ToString());
                }
                catch { }
            }

            try
            {
                return (int)ViewState["BGCategoryId"];
            }
            catch
            {
                return 0;
            }
        
        }
        set { ViewState["BGCategoryId"] = value; }
    }

    protected string generateCategoryViewerTable()
    {
        if (ViewState["generateCategoryViewerTable"] == null)
        {
            //IList<MediaSearch> medias = KioskViewerDAL.DataObjects.DataAccess.ICategoryDao.GetAll_category();
            int totalRec = 0;
            int totalPage = 0;
            IList<MediaSearch> medias = KioskViewerDAL.DataObjects.DataAccess.IMediaSearchDao.SearchBackgound("", CategoryId, 0, 20, ref totalRec, ref totalPage);
            ViewState["generateCategoryViewerTable"] = generateDynamicTable(3, medias, "width: 100%;text-align: center;", "text-align: center; width: 20%");
        }
        //Microsoft.SqlServer.Server.        
        return (string)ViewState["generateCategoryViewerTable"];
    }

    private string generateDynamicTable(int noCol, IList<MediaSearch> medias, string tblStyle, string cellStyle)
    {
        StringBuilder dTable = new StringBuilder(string.Format("<table style='{0}'>", tblStyle));

        // how many rows
        int noRows = ((medias.Count - 1) / noCol) - 1;
        if (noRows < 0) noRows = 0;

        int cellCount = 0;

        for (int currentRow = 0; currentRow <= noRows; currentRow++)
        {
            dTable.Append("<tr>");
            for (int currentCol = 0; currentCol <= noCol; currentCol++)
            {
                dTable.Append(string.Format("<td style=''>", cellStyle));
                if (cellCount <= medias.Count)
                {
                    try
                    {
                        //dTable.Append(string.Format("current Row: {0}; current col {1}", currentRow, currentCol));
                        //dTable.Append(string.Format("cell: {0}; current col {1}", cellCount, (currentRow * noCol) + currentCol));
                        MediaSearch currentmedias = (MediaSearch)medias[cellCount];
                        //String stringFlash = Common.FlashObj.Replace("%%Label%%", "Song").Replace("%%Link%%", "http://localhost:4792/KioskViewer/BKMainCategory.aspx");            
                        String stringFlash = Common.FlashObj.Replace("%%Label%%", currentmedias.Media_code).Replace("%%Link%%", "MediaPlayer.aspx?mediaId=" + currentmedias.Media_id.ToString());
                        dTable.Append(stringFlash);
                    }
                    catch { }
                }
                dTable.Append("</td>");
                cellCount += 1;

            }
            dTable.Append("</tr>");
        }
        return dTable.Append("</table>").ToString();
    }
}
